package com.it.test2;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MyRunnable implements Runnable {
    static int gift = 100;
    static Lock lock = new ReentrantLock();
    @Override
    public void run() {
        while (true) {
            lock.lock();
            try {
                if(gift <= 10) {
                    System.out.println(Thread.currentThread().getName()+"发现礼物小于10份，无法送出");
                    break;
                }else{
                    gift--;
                    System.out.println(Thread.currentThread().getName()+"送出了一份礼物,还剩"+ gift +"个礼物");
                }
            } finally {
                //无论怎样，锁都会被释放
                lock.unlock();
            }
        }
    }
}
